package projectiles;

import java.awt.Dimension;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

import core.ShootingUnit;
import core.Vector;
import enemies.Enemy;

/**
 * @author onbekend
 * @version 1.1
 * @since 2011-04-11
 */
public class Missile extends Projectile {

	private static final ImageIcon image = new ImageIcon("images/missile.gif");
	private static final int damage = 100;
	private static final int speed = 1250;

	private Enemy des; // enemy object.

	/**
	 * 
	 * @param origin het beginpunt
	 * @param position de positie
	 * @param viewAngle de richting van het projectiel
	 * @param destn het eindpunt
	 */
	/* /// Constructor //////////////////////////////////////// */
	public Missile(ShootingUnit origin, Point2D.Double position, int viewAngle, Enemy destn) {
		super(origin, position, viewAngle, new Dimension(Missile.image.getIconWidth(), Missile.image.getIconHeight()),
				Missile.image, Missile.damage, destn.getPosition(), Missile.speed);
		this.des = destn;
	}

	/**
	 * Past de missile aan
	 * @see Projectile#update()
	 *
	 * @return altijd true
	 */
	/* /// update //////////////////////////////////////// */
	public boolean update() {
		// missile aanpassen
		// controleren of enemy nog health heeft
		if (des.getHealth() > 0) {
			// de vector richting updaten.
			super.direction = new Vector(des.getPosition().x - super.position.x, des.getPosition().y - super.position.y)
					.normalize();
			calcViewAngle();
		}

		// superklasse aanroepen
		super.update();
		return true;
	}

	/* /// calcViewAngle //////////////////////////////////////// */
	private void calcViewAngle() {
		// Viewangle aanpassen, moet wijzen naar enemy

		Point2D.Double enemyPos = des.getPosition();
		double x = enemyPos.getX() - position.x;
		double y = enemyPos.getY() - position.y;
		// vijand links van toren
		if (x < 0) {
			// vijand hoger dan toren
			x *= -1;
			if (y < 0) {
				y *= -1;
				super.viewAngle = (int) Math.toDegrees((Math.atan((y / x)))) + 90;
			}
			// vijand lager dan toren
			else if (y > 0) {
				super.viewAngle = (int) Math.toDegrees((Math.atan((x / y))));
			}
			// vijand rechts van toren
		} else if (x >= 0) {
			if (y < 0) {
				y *= -1;
				super.viewAngle = (int) Math.toDegrees((Math.atan((x / y)))) + 180;
			} else if (y > 0) {
				super.viewAngle = (int) Math.toDegrees((Math.atan((y / x)))) + 270;
			}
		}

	}

}
